import Ember from 'ember';
import Changeset from 'ember-changeset';
import RoomValidations from '../../../validations/room';
import lookupValidator from 'ember-changeset-validations';

export default Ember.Controller.extend(RoomValidations,{
  constants:Constants,
  //默认楼层
  defaultFloor:Ember.computed('roomInfo.floor',function(){
      return this.get('roomInfo.floor');
  }),
  defaultBuilding:Ember.computed('roomInfo.build',function(){
    if(this.get('roomInfo.build')){
      return this.get('roomInfo.build');
    }else{
      return this.get('roomInfo.floor.building');
    }

  }),
  roomModel:Ember.computed("roomInfo",function(){
    var model = this.get("roomInfo");
    if (!model) {
        return null;
    }
    return new Changeset(model, lookupValidator(RoomValidations), RoomValidations);
  }),
  actions:{
    invalid() {
        //alert("error");
    },
    //编辑按钮
    detailEditClick:function(){
      this.set('detailEdit',true);
      // console.log("test save");
      // var _self = this;
      // let nursingproject = this.get('store').createRecord('nursingproject',{});
      // console.log("nursingproject get",nursingproject);
      // let i1 = _self.get('store').createRecord('nursingprojectitem', { price: 10 });
      // let i2 = _self.get('store').createRecord('nursingprojectitem', { price: 20 });
      // let services = new Ember.A();
      // nursingproject.get("services").pushObject(i1);
      // nursingproject.get("services").pushObject(i2);
      // // nursingproject.set("services",services);
      // console.log("nursingproject iss:",nursingproject);
      // nursingproject.save();
    },
    //取消按钮
    detailCancel:function(){
      var id=this.get('id');
      var editMode=this.get('editMode');
      if(id&&editMode=='edit'){
        this.set('detailEdit',false);
        this.get('roomInfo').rollbackAttributes();
        var route=App.lookup('route:business.mainpage.room-detail');
        route.refresh();//刷新页面
      }else{
        this.set('roomInfo.floor','');
        this.get('roomInfo').rollbackAttributes();
        let mainpageController = App.lookup('controller:business.mainpage');
        mainpageController.switchMainPage('room-management');
      }
    },
    //保存房间
    saveRoom(){

      var _self=this;
      var editMode=this.get('editMode');
      var roomModel=this.get('roomModel');
        roomModel.validate().then(function(){

          if(roomModel.get('errors.length')===0){
            App.lookup('controller:business.mainpage').openPopTip("正在保存");
            roomModel.set('isPublicFlag',0);
            roomModel.save().then(function(){
              App.lookup('controller:business.mainpage').showPopTip("保存成功");
              if(editMode=='add'){
                var mainpageController = App.lookup('controller:business.mainpage');
                mainpageController.switchMainPage('room-management');
              }else{
                  _self.set('detailEdit',false);
              }
            });
          }else{
            roomModel.set("validFlag",Math.random());
          }
      });
    },
    //删除按钮
    delById : function(room) {
      var _self = this;
      App.lookup('controller:business.mainpage').showConfirm("是否确定删除此房间记录",function(){
        _self.send('cancelPassSubmit',_self.get('roomInfo'));
      });
    },
    //选择楼层
    selectFloor(floor) {
        this.set('roomInfo.floor', floor);
        this.set('roomModel.floor',floor);
    },
    //选择楼宇
    selectBuilding(building) {
      this.set('roomInfo.build',building);
      console.log('name',building.get('name'));
      let _self=this;
      this.store.query('floor',{filter:{'[building][id]':building.get('id')}}).then(function(floorList){
        floorList.forEach(function(floor){
          floor.set('namePinyin',pinyinUtil.getFirstLetter(floor.get("name")));
        });
        _self.set('floorList',floorList);
      });
    },
    //选择房间类型
    roomSelect(roomType){
      this.set('roomInfo.roomType',roomType);
      this.set('roomModel.roomType',roomType);
    },
    //选择房间朝向
    orientationSelect(orientation){
      this.set('roomInfo.orientation',orientation);
      this.set('roomModel.orientation',orientation);
    },
    //弹窗取消
    invitation(){
      this.set('showpopInvitePassModal',false);
    },
    //弹窗确定，删除记录
    cancelPassSubmit(room){
      App.lookup('controller:business.mainpage').openPopTip("正在删除");
			this.set("showpopInvitePassModal",false);
      room.set("delStatus", 1);
      room.save().then(function() {
          App.lookup('controller:business.mainpage').showPopTip("删除成功");
          var mainpageController = App.lookup('controller:business.mainpage');
          mainpageController.switchMainPage('room-management');

      });
		},
  }
});
